var funObj={
	inits:function(){
		this.ajax();
		this.addressFun();//修改，添加地址
		this.addressConfirmFun();//确认按钮方法
		
		this.addOrRed();//加一或减一
		//console.log(common.getCookie("userName"))
	},
	Interface:{
		getProvince:"http://ww.yc630.com/api/index.php/Buyer/User/getProvince", //获取省
		getCity:"http://ww.yc630.com/api/index.php/Buyer/User/getCity",   //获取市
		getArea:"http://ww.yc630.com/api/index.php/Buyer/User/getArea",  //获取区县
		getaddress:"http://ww.yc630.com/api/index.php/Buyer/Order/getaddress",  //获取收获地址
		addAddress:"http://ww.yc630.com/api/index.php/Buyer/User/addAddress",  //添加收获地址
		changeAddress:"http://ww.yc630.com/api/index.php/Buyer/User/changeAddress",  //修改收货地址
		createOrder:"http://ww.yc630.com/api/index.php/Buyer/Order/createOrder",   //确认订单
		orderGoodlist:"http://ww.yc630.com/api/index.php/Buyer/Order/orderGoodlist",    //获取订单详情
		freightCalculation:"http://ww.yc630.com/api/index.php/Buyer/Order/freightCalculation"    //计算运费
	},
	ajax:function(){
		common.ajaxFun(funObj.Interface.getProvince,"POST",{token:token},funObj.getProvinceFun); //省
		common.ajaxFun(funObj.Interface.getaddress,"POST",{token:token},funObj.getaddressFun);//获取收获人信息
		common.ajaxFun(funObj.Interface.orderGoodlist,"POST",{token:token,orderid:common.Geturl().orderid},funObj.orderGoodlistFun);//获取订单详情
	},
	orderGoodlistFun:function(res){
		var str="";
		//console.log(res)
		$.each(res.data.list,function(i,v){
			$.each(v.goods,function(a,b){
			var goods_attrs=b.goods_attr.split("；");
			console.log(goods_attrs)
			str+='<div class="row row'+a+'">'
			str+='<input type="hidden" class="okGoods" seller_id='+b.seller_id+' id='+b.id+' goods_nu='+b.goods_nu+' goods_id='+b.goods_id+' total_money='+b.total_money+' freight='+b.freight+'>'
				str+='<div class="td Orderfirst">'
					str+='<div class="commodityImg"><img src="'+b.goods_image+'" alt=""></div>'
					str+='<div class="commodityIntroduce">'+b.goods_name+'</div>'
				str+='</div>'
				str+='<div class="td Ordersecond">'
					str+='<div class="size">'
						$.each(goods_attrs,function(c,d){
							str+='<div>'+d+'</div>'
						});
					str+='</div>'
				str+='</div>'
				str+='<div class="td Orderthird">'
					str+='<div class="commodityPrice">¥<span>'+b.goods_price+'</span>元</div>'
				str+='</div>'
				str+='<div class="td Orderfourth">'
					str+='<div class="commodityNum">'
						str+='<div class="reduce control">－</div>'
						+'<input type="text" class="commodityNumber" value="'+b.goods_nu+'">'
						str+='<div class="add control">＋</div>'
					str+='</div>'
				str+='</div>'
				str+='<div class="td Orderfifth">'
					str+='<div class="commoditymoney">¥'+b.total_money+'</div>'
				str+='</div>'
				str+='<div class="td Ordersixth">'
				if (b.freight_status==0) {//买家
					str+='<div class="commoditymoney freight yunfeis" seller_id='+b.seller_id+' goods_id='+b.goods_id+'>¥'+b.freight+'</div>'
					str+='<div class="seller">买家承担</div>'
				}else if (b.freight_status==1) {
					str+='<div class="commoditymoney freight yunfeis" seller_id='+b.seller_id+' goods_id='+b.goods_id+'>¥'+(0.00).toFixed(2)+'</div>'
					str+='<div class="seller">卖家承担</div>'
				}
				str+='</div>'
			str+='</div>';
			//console.log(b.freight_status)
			});
		});
	$("#confirmSp").html(str);
	funObj.TotalFun();  //计算总价和数量
	funObj.orderBtnFun();//确认下单	
	},
	getaddressFun:function(res){//获取收获地址方法
		var str="";
		$.each(res.data,function(k,v){
			str+='<li>'+
				'<input type="radio" name="address" id="address'+v.id+'" value='+k+'>'+
				'<label for="address'+v.id+'" class="addressName">'+v.username+'</label>'+
				'<span class="addressProvince" valueid='+v.province+'>'+v.province_name+'</span>'+
				'<span class="addressCity" valueid='+v.city+'>'+v.city_name+'</span>'+
				'<span class="addressStreet">'+v.address+'</span>'+
				'<span class="addressCode">'+v.postcode+'</span>'+
				'<span class="addressTel">'+v.phone+'</span>'+
				'<input type="hidden" value="'+v.id+'" class="hiddenids">'+
				'<input type="hidden" value="'+v.province+'" class="province1 hiddenids">'+
				'<input type="hidden" value="'+v.city+'" class="city1 hiddenids">'+
				'<span class="addressModify">［修改本地址］</span>'+
			'</li>';
		});
		$("#HarvestAddress ul#changeAdd").html(str);
		$('input[name="address"]').eq(0).attr("checked","checked");
	},
	addressConfirmFun:function(){
		$("#addressConfirm").on("click",function(){//点击确认
			var rad=$('input[name="address"]:checked').val(),
				liOk=$("#changeAdd li").eq(rad);

			var userName=liOk.children(".addressName").text(),
				adressOk=liOk.children(".addressProvince").text()+" "+liOk.children(".addressCity").text()+" "+liOk.children(".addressStreet").text(),
				codeOk=liOk.children(".addressCode").text(),
				telOk=liOk.children(".addressTel").text();
				$("#userNameOk").text(userName);
				$("#adressOk").text(adressOk);
				$("#codeOk").text(codeOk);
				$("#telOk").text(telOk);
				$("#provincialid").val($(".addressProvince").attr("valueid"));
				$("#cityid").val($(".addressCity").attr("valueid"));
				$("#addressid").val(liOk.children(".addressStreet").text());
			$("#changeAdd,.addAdress").hide();
			$("#changeAddBtn,#okAdd").show();
		});	

		$("#changeAddBtn").on("click",function(){//点击确认
			$("#changeAdd,.addAdress").show();
			$("#changeAddBtn,#okAdd").hide();

		})
	},
	addressFun:function(){
		var flag=1;
		$("#HarvestAddress").on("click",".addressModify",function(){//修改地址
			flag=1;//修改地址
			$("#AddressDialog h3").text("修改本地址");
		common.ajaxFun(funObj.Interface.getCity,"POST",{provinceid:$(this).siblings(".province1").val()},funObj.getCityFun); //市
			$("#username").val($(this).siblings(".addressName").text());
			$("#address").val($(this).siblings(".addressStreet").text());
			$("#postcode").val($(this).siblings(".addressCode").text());
			$("#mobile").val($(this).siblings(".addressTel").text());
			$("#hiddenId").val($(this).siblings(".hiddenids").val());
			$("#province").val($(this).siblings(".province1").val());
			$("#region").val($(this).siblings(".city1").val());


			$("#AddressDialog,.mask").show();
		});
		$("#cancel").on("click",function(){
			$("#AddressDialog,.mask").hide();
			$("#username").val("");
			$("#address").val("");
			$("#postcode").val("");
			$("#mobile").val("");
			$("#hiddenId").val("");
			$("#province").val("");
			$("#region").val("");
		})
		$(".addressAdd").on("click",function(){//添加新地址
			flag=2;
			$("#AddressDialog h3").text("添加地址");
			$("#username").val("");
			$("#address").val("");
			$("#postcode").val("");
			$("#mobile").val("");
			$("#hiddenId").val("");
			$("#province").val("");
			$("#region").val("");
			common.ajaxFun(funObj.Interface.getProvince,"POST",{},funObj.getProvinceFun); 
			$("#AddressDialog,.mask").show();
		});
		$("#PreservationBtn").on("click",function(){//弹窗确认按钮
			var username=$("#username").val(),
				province=$("#province").val(),
				city=$("#region").val(),
				area=$("#city").val(),
				address=$("#address").val(),
				mobile=$("#mobile").val(),
				postcode=$("#postcode").val(),
				id=$("#hiddenId").val();
			if(username==""){
				alert("收货人姓名不能为空");
				return;
			}else if (province=="") {
				alert("收货人省份不能为空");
				return;
			}else if (city=="") {
				alert("收货人城市不能为空");
				return;
			}else if (address=="") {
				alert("收货人详细地址不能为空");
				return;
			}else if (mobile=="") {
				alert("收货人手机不能为空");
				return;
			}
			else if (postcode=="") {
				alert("收货人邮编不能为空");
				return;
			}
			//console.log(area)
			if(area==""){
				if (flag==1) {
					var obj1={
						username:username,
						province:province,
						city:city,
						address:address,
						phone:mobile,
						postcode:postcode,
						id:id,
						token:token
					}
					common.ajaxFun(funObj.Interface.changeAddress,"POST",obj1,function(res){
						$("#AddressDialog,.mask").hide();
						common.ajaxFun(funObj.Interface.getaddress,"POST",{token:token},funObj.getaddressFun);//获取收获人信息
					});
				}else if (flag==2) {
					var obj2={
						username:username,
						province:province,
						city:city,
						address:address,
						phone:mobile,
						postcode:postcode,
						token:token
					}
					common.ajaxFun(funObj.Interface.addAddress,"POST",obj2,function(res){
						$("#AddressDialog,.mask").hide();
						common.ajaxFun(funObj.Interface.getaddress,"POST",{token:token},funObj.getaddressFun);//获取收获人信息
					});
				}
			}else {
				if (flag==1) {
					var obj1={
						username:username,
						province:province,
						city:city,
						address:address,
						phone:mobile,
						area:area,
						postcode:postcode,
						id:id,
						token:token
					}
					common.ajaxFun(funObj.Interface.changeAddress,"POST",obj1,function(res){
						$("#AddressDialog,.mask").hide();
						common.ajaxFun(funObj.Interface.getaddress,"POST",{token:token},funObj.getaddressFun);//获取收获人信息
					});
				}else if (flag==2) {
					var obj2={
						username:username,
						province:province,
						city:city,
						address:address,
						area:area,
						phone:mobile,
						postcode:postcode,
						token:token
					}
					common.ajaxFun(funObj.Interface.addAddress,"POST",obj2,function(res){
						$("#AddressDialog,.mask").hide();
						common.ajaxFun(funObj.Interface.getaddress,"POST",{token:token},funObj.getaddressFun);//获取收获人信息
					});
				}
			}
		});
	},
	getProvinceFun:function(res){
		var str="",val,kong="<option>请选区县</option>";

			$.each(res.data,function(k,v){
				str+="<option value="+k+">"+v+"</option>";
			})
			$("#province").append(str);
			$("#province").on("change",function(){
				val=$(this).val();
				$("#city").html(kong);
				common.ajaxFun(funObj.Interface.getCity,"POST",{provinceid:val},funObj.getCityFun); //市
			})
	},
	getCityFun:function(res){
		var str="",val;
			$.each(res.data,function(k,v){
				str+="<option value="+k+">"+v+"</option>";
			});
			$("#region").html(str);
			$("#region").on("change",function(){
				val=$(this).val();

				common.ajaxFun(funObj.Interface.getArea,"POST",{cityid:val},funObj.getAreaFun); //市
			})
	},
	getAreaFun:function(res){
		var str="",val;
		$.each(res.data,function(k,v){
			str+="<option value="+k+">"+v+"</option>";
		});
		$("#city").html(str);
	},
	addOrRed:function(){
		$("#confirmSp").on("click",".add",function(){
			var goods_price=$(this).parents("div.Orderfourth").prev(".Orderthird").find("span").text();
			var valNum=$(this).prev("input.commodityNumber").val();
			valNum++;
			$(this).prev("input.commodityNumber").val(valNum);
			price=goods_price*valNum;
			$(this).parents("div.Orderfourth").next(".Orderfifth").children("div.commoditymoney").html("¥"+price.toFixed(2))
			$(this).parents("div.Orderfourth").siblings(".okGoods").attr("goods_nu",valNum);
			$(this).parents("div.Orderfourth").siblings(".okGoods").attr("total_money",price.toFixed(2));
				funObj.TotalFun();
		});
		$("#confirmSp").on("click",".reduce",function(){
			var valNum=$(this).next("input.commodityNumber").val();
			if (valNum<=0) {
				alert("数量不能小于0");
				return false;
			}else {
				var goods_price=$(this).parents("div.Orderfourth").prev(".Orderthird").find("span").text();
				valNum--;
				$(this).next("input.commodityNumber").val(valNum);
				price=goods_price*valNum;
				$(this).parents("div.Orderfourth").next(".Orderfifth").children("div.commoditymoney").html("¥"+price.toFixed(2));
				$(this).parents("div.Orderfourth").siblings(".okGoods").attr("goods_nu",valNum);
				$(this).parents("div.Orderfourth").siblings(".okGoods").attr("total_money",price.toFixed(2));
			
				funObj.TotalFun();
			}
		});
	},
	TotalFun:function(){
			var totNum=0,totMon=0,freightMon=0;
			//console.log($("input.okGoods"))
			$("#confirmSp input.okGoods").each(function() {
	           totNum+=Number($(this).attr("goods_nu"));
	           totMon+=Number($(this).attr("total_money"));

	           if ($(this).attr("freight")=="null") {
	           	freightMon+=0;
	           }else {
	           	freightMon+=Number($(this).attr("freight"));
	           };
	           
	        });
	        $("#huopinMon").html("¥"+totMon.toFixed(2));
	        $("#yunfei").html("¥"+freightMon.toFixed(2));
	        $("#zonger").html("¥"+(totMon+freightMon).toFixed(2))
	        $("#TotalNum").html(totNum);
	        $("#TotalMon").html(totMon.toFixed(2));
	},
	freightCalculationFun:function(eve){//计算运费
			var freightCalculationObj={},goods_ids="";
			$(".okGoods").each(function(i,v){
				goods_ids+=$(this).attr("goods_id")+":"+$(this).attr("goods_nu")+",";
			})
			goods_ids=goods_ids.substring(0,goods_ids.length-1);
			freightCalculationObj.orderid=common.Geturl().orderid;
			freightCalculationObj.provincial=$("#provincialid").val();
			freightCalculationObj.city=$("#cityid").val();
			freightCalculationObj.toatlmoney=$("#TotalMon").text();
			freightCalculationObj.goods_ids=goods_ids;
			freightCalculationObj.token=token;
			// common.ajaxFun(funObj.Interface.freightCalculation,"POST",freightCalculationObj,function(res){//计算运费   暂时不用
			// 	if (res.code==0) {
			// 	var okGoods=$("input.okGoods"),
			// 	yunfeis=$("div.yunfeis"),
			// 	tofreight=0,
			// 	huoMoney=0;
			// 	len=yunfeis.length;
			// 	console.log(res.data)
			// 	$.each(res.data.freight,function(i,v){
			// 		yunfeis.each(function(a,b){
			// 			var that=$(this);
			// 			if (i==$(this).attr("seller_id")) {
			// 				$.each(v,function(k,l){
			// 					if (k==that.attr("goods_id")) {
			// 						that.html("¥"+l.toFixed(2));
			// 					};
			// 				});
			// 			};
			// 		});
			// 		okGoods.each(function(a,b){
			// 			var that=$(this);
			// 			if (i==$(this).attr("seller_id")) {
			// 				$.each(v,function(k,l){
			// 					if (k==that.attr("goods_id")) {
			// 						that.attr("freight",l.toFixed(2));
			// 						tofreight+=l;
			// 					};
			// 				});
			// 			};
			// 		});
			// 	});
			// 	okGoods.each(function(a,b){
			// 		huoMoney+=parseInt($(this).attr("total_money"));
			// 	});
			// 	$("#yunfei").html("¥"+tofreight.toFixed(2));
			// 	$("#zonger").html("¥"+(huoMoney+tofreight).toFixed(2));
			// 	$("#TotalMon").html("¥"+(huoMoney+tofreight).toFixed(2));
			// }else {
			// 	alert(res.msg)
			// };
		//});
	},
	orderBtnFun:function(){//确认订单

		
		$("#expresses_type").on("change","input",function(eve){//计算运费
			funObj.freightCalculationFun();
			var freight_status=$(this).val();
			console.log(freight_status)
			if (freight_status==2) {
				$(".seller").html("卖家承担");
			}else {
				$(".seller").html("买家承担");
			};
		});

		

		$("#orderBtn").on("click",function(){
			var sunObj={},goods_ids="",seller_id="";
			if ($("#provincialid").val()=="") {
				alert("请确认收货地址！");
				return false;
			};
			$("#confirmSp input.okGoods").each(function() {
				goods_ids+=$(this).attr("id")+":"+$(this).attr("goods_nu")+":"+$(this).attr("total_money")+","
				
			});
			sunObj.recipients=$("#userNameOk").text();//收获人
			sunObj.provincial=$("#provincialid").val();//省份ID
			sunObj.city=$("#cityid").val();  ///城市ID
			sunObj.address=$("#addressid").val();  //详细地址
			sunObj.postcode=$("#codeOk").text();  //邮编
			sunObj.mobile=$("#telOk").text();  //收获人电话
			sunObj.paytype=$("#payType input:checked").val();//支付方式 0未知，1 微信、2支付宝、3银联、4现金
			sunObj.coupon_id=1;//优惠券id，选填（该功能暂时不用开发，先默认传1）
			sunObj.expresses_type=$("#expresses_type input:checked").val(); //0普通快递，1顺丰快递，2卖家包邮
			sunObj.goods_ids=goods_ids.substring(0,goods_ids.length-1);
			sunObj.orderid=common.Geturl().orderid;
			sunObj.total_money=($("#huopinMon").text()).substring(1);  //不包含运费
			sunObj.freight=($("#yunfei").text()).substring(1);
			sunObj.real_money=($("#zonger").text()).substring(1);
			sunObj.remark=$("#remark").val();
			sunObj.inviteid=$("#inviteid").val();
			sunObj.token=token;
			console.log(sunObj)
		common.ajaxFun(funObj.Interface.createOrder,"POST",sunObj,function(res){
			if (res.code==0) {//订单提交成功
				alert(res.msg)
			}else {
				alert(res.msg);
				//window.location.href="./login/login.html"
			};
			//console.log(res)
		});//获取收获人信息

		})
	}	
}	
funObj.inits();